﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TRTC;
using TCommon;
using TCommon.Enum;
using System.Media;
using Newtonsoft.Json;
using PhilipService.Entity;
using PhilipService;
using System.Threading.Tasks;

namespace PhilipSupport
{
    public partial class CallComeForm : Form
    {
        private string userId;
        private long serviceId;
        private string name;
        private SoundPlayer player;
        private long customerId;
        private string deviceId;

        public CallComeForm(long customerId, string userId, string name, string information, long serviceId, string deviceId)
        {
            InitializeComponent();
            this.userId = userId;
            this.name = name;
            this.customerId = customerId;
            this.serviceId = serviceId;
            label_name.Text = name;
            label_information.Text = information;
            this.deviceId = deviceId;
            player = new SoundPlayer();
            player.SoundLocation = System.AppDomain.CurrentDomain.BaseDirectory + "\\sound.wav";
            PhilipContext.callComeForm = this;
        }

        public void CancelCall() {
            player.Stop();
            PhilipContext.callComeForm = null;
            this.Close();
        }

        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }

        private void CallComeForm_Resize(object sender, EventArgs e)
        {
            SetWindowRegion();
        }

        private void CallComeForm_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void btn_refuse_Click(object sender, EventArgs e)
        {
            //var resp = new SupportCallResponse()
            //{
            //    Type = "philipsUser",
            //    UserId = userId
            //};
            //var msg = JsonConvert.SerializeObject(resp);
            //PhilipContext.supportMain.SendMsg(msg);
            var manager = new RestServiceManager();
            manager.RefuseService(this.customerId);
            player.Stop();
            PhilipContext.callComeForm = null;
            this.Close();
        }

        private void btn_accept_Click(object sender, EventArgs e)
        {
            PhilipContext.callComeForm = null;
            player.Stop();
            var manager = new RestServiceManager();
            var res = manager.GetDeviceInfo(deviceId, serviceId);
            var form = new VideoMainForm(int.Parse(TCommon.AppContext.LocalRoomId), TCommon.AppContext.LocalUserName, TCommon.AppContext.LocalUserName, TCommon.AppContext.UserSign, UserRole.Host);
            form.FormClosed += Form_FormClosed;
            form.NoteChanged += Form_NoteChanged;
            form.RemoveUserClicked += Form_RemoveUserClicked;
            form.SetPhilipsMode(serviceId, res);
            form.Show();
            if (PhilipContext.isAvailable)
            {
                PhilipContext.supportMain.SetOnlineStatus(Entity.OnLineStatus.Busy);
                PhilipContext.supportMain.SetStatusButtonsEnable(false);
            }

            var resp = new SupportCallResponse()
            {
                Type = "philipsUser",
                UserId = userId
            };
            var msg = JsonConvert.SerializeObject(resp);
            PhilipContext.supportMain.SendMsg(msg);
            this.Hide();
        }

        private void Form_RemoveUserClicked(string userId, long serviceId)
        {
            var req = new KickUserRequest()
            {
                Type = "kickuser",
                UserId = userId,
                ServiceId = serviceId
            };
            var msg = JsonConvert.SerializeObject(req);
            PhilipContext.supportMain.SendMsg(msg);
        }

        private void Form_NoteChanged(long serviceId, string note)
        {
            Task.Factory.StartNew(() =>
            {
                var manager = new RestServiceManager();
                manager.AddOrUpdateServiceNote(serviceId, note);
            });          
        }

        private void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (PhilipContext.isAvailable)
            {
                var resp = new SupportCallResponse()
                {
                    Type = "endmeeting",
                    UserId = userId
                };
                var msg = JsonConvert.SerializeObject(resp);
                PhilipContext.supportMain.SendMsg(msg);
                var manager = new RestServiceManager();
                manager.EndService(serviceId);
                PhilipContext.supportMain.SetStatusButtonsEnable(true);
                PhilipContext.supportMain.SetOnlineStatus(Entity.OnLineStatus.Online);
                PhilipContext.supportMain.ShowServiceList();
            }
            this.Close();
        }

        private void CallComeForm_Load(object sender, EventArgs e)
        {
            player.PlayLooping();       
        }
    }
}
